﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.ADVERTISEMENT.Pages
{
    public partial class SlideShowTopBanner : System.Web.UI.Page
    {
        public string arrayScript = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                //gvItem.RepeatColumns = int.Parse(this.Settings["Columns.Count"].ToString());
                this.GridDataBinding();

                //RegisterScriptSlideShowImage();
            }
        }

        private void GridDataBinding()
        {
            Guid moduleID = new Guid(Request.QueryString["mid"]);

            // Get one type item for home.
            //DataSet ds = Components.Item.SelectAll(moduleID);
            //gvItem.DataSource = ds;
            //gvItem.DataBind();

            arrayScript = CreateScriptImageArray(Components.Item.SelectCollectionAll(moduleID));
        }

        private void RegisterScriptSlideShowImage()
        {
            string script =
                "<script language='javascript' type='text/javascript'>" + "\n" +
                    "window.addEvent('domready', function() {" + "\n\t" +
                        "/* thumbnails example */" + "\n\t\t" +
                        "new SlideItMoo({ itemsVisible: 3, // the number of thumbnails that are visible" + "\n\t\t" +
                            "currentElement: 0, // the current element. starts from 0. If you want to start the display with a specific thumbnail, change this" + "\n\t\t\t" +
                            "autoSlide: " + int.Parse(Request.QueryString["s"]) + ",//3000, // insert interval in milliseconds" + "\n\t\t\t" +
                            "thumbsContainer: 'thumbs'," + "\n\t\t\t" +
                            "elementScrolled: 'thumb_container'," + "\n\t\t\t" +
                            "overallContainer: 'gallery_container'" + "\n\t\t\t" +
                        "});" + "\n\t\t" +
                    "});" + "\n\t" +
                "</script>";

            //Page.RegisterClientScriptBlock("slideShow", script);
            ClientScript.RegisterClientScriptBlock(typeof(String), "slideShowTopBanner", script);
        }

        private string CreateScriptImageArray(List<Components.Item> listItems)
        {
            System.Text.StringBuilder str = new System.Text.StringBuilder();
            string imageUrl = "";
            string href = "";
            string target = "";
            string title = "";
            string imagePath = "";
            int width = 728;
            int height = 106;

            string template =
                "<a rel='lightbox' target='_blank' href='{0}'>" +
                    "<img src='{1}' width='{2}' height='{3}' title=''/>" +
                "</a>";

            for (int i = 0; i < listItems.Count; i++)
            {
                imagePath = listItems[i].ImageUrl;

                //if (imagePath.IndexOf('~') != -1)
                //{
                //    imagePath = imagePath.Substring(1);
                //}

                //B:: Chỉ hiển thị các tin tức có ảnh.
                if (imagePath.Length != 0)
                {
                    imageUrl = ResolveUrl(imagePath);
                    href = listItems[i].Url;
                    title = ""; //Server.HtmlEncode(CompactNewsSubTitle(listNewsInfos[i].SubTitle));
                    //width = listItems[i].Width;
                    //height = listItems[i].Height;

                    str.Append(string.Format(template, href, imageUrl, width, height));
                    str.AppendLine();

                    //if (i < listItems.Count - 1)
                    //{
                    //    str.Append(",");
                    //    //str.AppendLine();
                    //}
                }
            }

            string result = str.ToString();
            int length = result.Length;

            if (result.Substring(length - 1).IndexOf(",") != -1)
            {
                result = result.Substring(0, length - 1);
            }

            return result;
        }

    }
}
